﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class CustomermanagementS : CustomermanagementSI
    {
        private readonly WmssystemContext myDb;

        public CustomermanagementS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }
        public Task<List<Su_Customermanagement>> Qu()
        {
            return myDb.Su_Customermanagement.ToListAsync();
        }
        public Task<Su_Customermanagement> QuName(int Name)
        {
            return myDb.Su_Customermanagement.FirstOrDefaultAsync(s => s.S_CuNumber == Name);
        }
        //添加
        public Task<int> Add(Su_Customermanagement cusAdd)
        {
            myDb.Su_Customermanagement.Add(cusAdd);
            return myDb.SaveChangesAsync();
        }
        //分页模糊查询
        public Task<List<Su_Customermanagement>> FenYe(int pageSize, int currentPage, string Key = null, string Status = null, string Industry = null, string Person = null)
        {
            var query = myDb.Su_Customermanagement.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Key))
            {
                query = query.Where(c => c.S_CuCode.Contains(Key) || c.S_CuName.Contains(Key));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.S_CuStatus.Contains(Status));
            }
            if (!string.IsNullOrEmpty(Industry))
            {
                query = query.Where(c => c.S_CuIndustry.Contains(Industry));
            }
            if (!string.IsNullOrEmpty(Person))
            {
                query = query.Where(c => c.S_CuContactPerson.Contains(Person));
            }
            query = query.Where(c => c.S_CuisDelete == true);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        //修改
        public Task<int> Upd(Su_Customermanagement cusUpd)
        {
            myDb.Su_Customermanagement.Update(cusUpd);
            return myDb.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDb.Su_Customermanagement.FindAsync(id);
            // 更新实体的删除状态
            entity.S_CuisDelete = isDeleted;
            // 标记实体为已修改状态
            myDb.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDb.SaveChangesAsync();
        }
    }
}
